using System;
using System.Collections.Generic;
using System.Text;

namespace csharp.rad.general
{
    public class Permissions
    {
        private List<Permission> permissionsItems = new List<Permission>();
        private Database myDatabase;

        public List<Permission> Items
        {
            get
            {
                return permissionsItems;
            }
            set
            {
                permissionsItems = value;
            }
        }

        public Permissions(Database myDatabase)
        {
            this.myDatabase = myDatabase;
        }

        /// <summary>
        /// Get all permissions from specified group
        /// </summary>
        /// <param name="value">The GroupId</param>
        /// <remarks>value = null, get all permissions</remarks>
        /// <returns></returns>
        public void GetData(String value)
        {
            String commandText;
            String tableName = "actions";
            Database.Result databaseResult = new Database.Result();
            System.Data.DataSet sqlDataSet = new System.Data.DataSet();

            //note: value = 0, get all permissions
            if (value == null)
            {
                //Get all permissions
                commandText = "SELECT action_id, "
                            + "       action_name "
                            + "FROM actions "
                            + "ORDER BY action_id";
            }
            else
            {
                //Get permissions of group
                commandText = "SELECT action_id, "
                            + "       action_name "
                            + "FROM actions "
                            + "INNER JOIN permissions "
                            + "ON actions.action_id = permissions.permission_action_id "
                            + "WHERE (permissions.permission_group_id = '" + value + "') "
                            + "ORDER BY action_id";
            }
            sqlDataSet = myDatabase.ExecuteQuery(commandText, tableName, out databaseResult);
            
            permissionsItems.Clear();
            foreach (System.Data.DataRow dataRecord in sqlDataSet.Tables[tableName].Rows)
            {
                Permission permissionItem = new Permission(myDatabase);
                permissionItem.ActionId = System.Convert.ToString(dataRecord["action_id"]);
                permissionItem.ActionName = System.Convert.ToString(dataRecord["action_name"]);
                permissionsItems.Add(permissionItem);
            }
            sqlDataSet.Dispose();
        }

        /// <summary>
        /// Find "action" in the "permissions" collection
        /// </summary>
        /// <param name="value">The ActionId</param>
        /// <returns></returns>
        public Permission Find(String value)
        {
            foreach (Permission itemPermission in permissionsItems)
            {
                if (itemPermission.ActionId == value)
                {
                    return itemPermission;
                }
            }
            return null;
        }

        public void Clear()
        {
            permissionsItems.Clear();
        }
    }
}
